package com.jcl.recruit.api.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.user.service.UserAuthorityService;
import com.jcl.recruit.user.service.UserService;
import com.jcl.recruit.user.vo.UserSearchEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * @ClassName UserController

 * @Author yyliu
 * @Date 2018/9/12
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserAuthorityService userAuthorityService;

    /* *
     * @Author yyliu
     * @Description 所有用户
     *
     * @Date 19:01 2018/9/12
     * @Param [result]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public ApiResponse pageList(@RequestBody UserSearchEntity entity){

        entity.setType(UserSearchEntity.TYPE_ALL);
        PageResult result = userService.pageList(entity, entity.getToPage(), DEFAULT_PAGE_SIZE);

        return ApiResponse.success(result);
    }

    /**
     * 用户查找
     * @param ishr
     * @param subcomany_id
     * @param department_id
     * @param realname
     * @return
     */
    @RequestMapping("/find")
    public Map<String,Object> find(Integer ishr,Integer subcomany_id,Integer department_id,String realname,
                                   @RequestParam(value = "page", required = false, defaultValue = "1") String toPage){
        Map<String,Object> map = new LinkedHashMap<>();
        PageHelper.startPage(Integer.valueOf(toPage),DEFAULT_PAGE_SIZE);
        List<Map<String,Object>> userList = userService.selectUsersByCondition(realname,ishr,subcomany_id,department_id);
        PageInfo pageInfo = new PageInfo(userList);
        map.put("type","success");
        map.put("message","执行成功");
        map.put("users",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    /**
     * 判断用户权限状态
     * @return
     */
    @RequestMapping(value = "/authority/status",method = RequestMethod.GET)
    public ApiResponse loginToken(){

        String userId = RequestUtils.getUserId();
        Map<String,Object> map = userAuthorityService.getUserAuthority(userId);

        return ApiResponse.success(map);
    }
}
